package com.egg.ylt.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.egg.ylt.R;
import com.egg.ylt.Utils.DividerItemDecoration;
import com.egg.ylt.activity.ACT_LookBigPicture;
import com.egg.ylt.pojo.DiaryPhotoEntity;
import com.google.gson.Gson;
import com.yonyou.framework.library.adapter.lv.ViewHolderListView;
import com.yonyou.framework.library.common.utils.ListUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * sichaoqi on 2018/12/4
 */
public class ADA_GrowthDiaryList extends RecyclerView.Adapter<ADA_GrowthDiaryList.GrowthDiaryViewHolder> {


    private boolean isSelect = false;
    private OnItemCancelClickListener onItemCancelClickListener;
    private OnItemClickListener itemClickListener;
    private List<ADA_GrowthDiaryRecycler> adapterList;
    private Context mContext;
    private LayoutInflater mInflater;
    private List<DiaryPhotoEntity.ListBean> mList;
    private ADA_GrowthDiaryRecycler ada_growthDiaryRecycler;

    public ADA_GrowthDiaryList(Context context) {
        adapterList = new ArrayList<>();
        this.mContext = context;
        mInflater = LayoutInflater.from(mContext);
        mList = new ArrayList<>();
    }

    public void setDataList(List<DiaryPhotoEntity.ListBean> list){
        if (mList != null){
            mList.clear();
        }
        if (adapterList != null){
            adapterList.clear();
        }
        mList.addAll(list);
        notifyDataSetChanged();
    }

    public void setDataClear(){
        mList.clear();
        adapterList.clear();
        notifyDataSetChanged();
    }

    public void setClearBtn(){
        if (adapterList != null){
            adapterList.clear();
        }
        notifyDataSetChanged();
    }
    @Override
    public GrowthDiaryViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new GrowthDiaryViewHolder(mInflater.inflate(R.layout.item_growth_diary_list, parent, false));
    }

    @Override
    public void onBindViewHolder(final GrowthDiaryViewHolder holder, final int position) {

        final DiaryPhotoEntity.ListBean listBean = mList.get(position);
        //照片展示日期
        holder.tvDate.setText(listBean.getDate());
        holder.btnSelect.setVisibility(View.VISIBLE);
        holder.btnDelete.setVisibility(View.GONE);
        holder.btnCancel.setVisibility(View.GONE);
        //选择
        holder.btnSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.btnSelect.setVisibility(View.GONE);
                holder.btnDelete.setVisibility(View.VISIBLE);
                holder.btnCancel.setVisibility(View.VISIBLE);
                adapterList.get(position).setSelected(true);
            }
        });

        //删除
        holder.btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = onDeletePhoto(position);
                onItemCancelClickListener.onItemClick(s);
            }
        });

        //取消
        holder.btnCancel.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.btnSelect.setVisibility(View.VISIBLE);
                holder.btnDelete.setVisibility(View.GONE);
                holder.btnCancel.setVisibility(View.GONE);
                adapterList.get(position).setCancelSelect();
            }
        });

        //展示图片的recyclerview
        //设置布局适配器
        holder.recyclerView.setLayoutManager(new GridLayoutManager(mContext, 3){
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        //recyclerview的分割距离
//        holder.recyclerView.addItemDecoration(new DividerItemDecoration(10));
        //适配器
        ada_growthDiaryRecycler = new ADA_GrowthDiaryRecycler(mContext);
        adapterList.add(ada_growthDiaryRecycler);
        holder.recyclerView.setAdapter(ada_growthDiaryRecycler);
        ada_growthDiaryRecycler.setDataList(listBean.getDateList());
        adapterList.get(position).setCancelSelect();
        //点击图片放大
        adapterList.get(position).setOnItemClickListener(new ADA_GrowthDiaryRecycler.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Intent mIntent = new Intent(mContext, ACT_LookBigPicture.class);
                Bundle mBundle = new Bundle();
                mBundle.putSerializable("listBean", listBean);
                mIntent.putExtra("bundle", mBundle);
                mIntent.putExtra("position", position);
                mContext.startActivity(mIntent);
            }
        });
    }

    /**
     * 删除照片
     * @param position
     */
    private String onDeletePhoto(int position) {
        ADA_GrowthDiaryRecycler recycler = adapterList.get(position);
        List<DiaryPhotoEntity.ListBean.DateListBean> dataList = recycler.getDataList();
        //待删除的集合
        List<String> idList = new ArrayList<>();
        for (int i=0; i < dataList.size(); i++){
            boolean selected = dataList.get(i).isSelected();
            if (selected){
                idList.add(dataList.get(i).getId());
            }
        }
        if (ListUtil.isListEmpty(idList)){
            return "";
        }
        //生成带删除设备的字符串
        Gson gson = new Gson();
        String deleteJson = gson.toJson(idList);
        return deleteJson;
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }


    static class GrowthDiaryViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_date)
        TextView tvDate;
        @BindView(R.id.btn_cancel)
        TextView btnCancel;
        @BindView(R.id.btn_delete)
        TextView btnDelete;
        @BindView(R.id.btn_select)
        TextView btnSelect;
        @BindView(R.id.linearLayout)
        LinearLayout linearLayout;
        @BindView(R.id.recycler_view)
        RecyclerView recyclerView;
        public GrowthDiaryViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


    /**
     * 删除interface
     */
    public interface OnItemCancelClickListener {
        void onItemClick(String text);
    }

    public void setOnItemCancelClickListener(OnItemCancelClickListener listener) {
        this.onItemCancelClickListener = listener;
    }

    public interface OnItemClickListener {
        void onClick(String text);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.itemClickListener = listener;
    }
}
